<?php


namespace IDock\Insurance\JDAL;

use Exception;
use IDock\Http\Response;
use IDock\IDock\IDockInterface;
use IDock\Insurance\JDAL\Bean\BasePremiumInquiry;
use IDock\Insurance\JDAL\Request\AuthorizationTokenRequest;
use IDock\Insurance\JDAL\Request\ENCPolicyDownloadRequest;
use IDock\Insurance\JDAL\Request\EPolicyDownloadRequest;
use IDock\Insurance\JDAL\Request\NonCarProductListRequest;
use IDock\Insurance\JDAL\Request\PolicyPaymentRequest;
use IDock\Insurance\JDAL\Request\UnderwritingRequest;
use IDock\Insurance\JDAL\Request\UploadFileRequest;
use IDock\Insurance\JDAL\Request\VehicleInfoCheckRequest;
use IDock\Insurance\JDAL\Request\VehicleInsureQueryRequest;
use IDock\Insurance\JDAL\Request\VehicleModelRequest;
use IDock\Insurance\JDAL\Request\VehicleQuoteMainRequest;
use IDock\Insurance\JDAL\Response\AuthorizationTokenResponse;
use IDock\Insurance\JDAL\Response\ENCPolicyDownloadResponse;
use IDock\Insurance\JDAL\Response\EPolicyDownloadResponse;
use IDock\Insurance\JDAL\Response\NonCarProductListResponse;
use IDock\Insurance\JDAL\Response\PolicyPaymentResponse;
use IDock\Insurance\JDAL\Response\UnderwritingResponse;
use IDock\Insurance\JDAL\Response\UploadFileResponse;
use IDock\Insurance\JDAL\Response\ValidateCarResponse;
use IDock\Insurance\JDAL\Response\VehicleInfoCheckResponse;
use IDock\Insurance\JDAL\Response\VehicleInsureQueryResponse;
use IDock\Insurance\JDAL\Response\VehicleModelResponse;
use IDock\Insurance\JDAL\Response\VehicleQuoteMainResponse;
use IDock\Visitor\EPolicyDownloadVisitor;
use IDock\Visitor\NonCar\NonCarInquiryVisitor;
use IDock\Visitor\NonCar\NonCarProductListVisitor;
use IDock\Visitor\PolicyInfoVisitor;
use IDock\Visitor\PolicyPaymentVisitor;
use IDock\Visitor\Premium\PremiumInquiryVisitor;
use IDock\Visitor\ProposalRecallVisitor;
use IDock\Visitor\ProposalStatusVisitor;
use IDock\Visitor\RenewalInquiryVisitor;
use IDock\Visitor\UnderwritingRecallVisitor;
use IDock\Visitor\UploadFileVisitor;
use IDock\Visitor\ValidateCarVisitor;
use IDock\Visitor\VehicleActualValueVisitor;
use IDock\Visitor\VehicleModelVisitor;


class JDALConstruct extends Builder implements IDockInterface
{
    public $config = [];

    public function __construct($config)
    {
        $this->config = $config;
        $this->appId = $config['appId'] ?? '';
        $this->aesSecret = $config['aesSecret'] ?? '';
        $this->md5Secret = $config['md5Secret'] ?? '';
    }

    /**
     * 登录
     * @return void
     */
    public function getAuthorizationToken(): AuthorizationTokenResponse
    {
        $request = new AuthorizationTokenRequest();
        $request->appId = $this->appId;

        /*** @var $response AuthorizationTokenResponse */
        $response = $this->execute($request);

        return $response;

    }

    /**
     * 续保查询
     */
    public function renewalInquiry(RenewalInquiryVisitor $renewalInquiryVisitor)
    {
        //Implement renewalInquiry() method.
    }

    /**
     * 车型查询
     * @param VehicleModelVisitor $vehicleModelVisitor
     * @return VehicleModelResponse
     */
    public function vehicleModel(VehicleModelVisitor $vehicleModelVisitor): VehicleModelResponse
    {
        $request = new VehicleModelRequest();
        $request->setVisitor($vehicleModelVisitor);

        /*** @var VehicleModelResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 车辆实际价格
     */
    public function vehicleActualValue(VehicleActualValueVisitor $vehicleActualValueVisitor): Response
    {
        //调用安城价格
        $response = new \IDock\Insurance\ACIC\Response\VehicleActualValueResponse();
        $response->setCode(200);
        $response->setVisitor($vehicleActualValueVisitor);
        $response->printerInit();
        return $response;
    }

    /**
     * 询价前置
     */
    public function preInquiry(PremiumInquiryVisitor $premiumInquiryVisitor)
    {
        //Implement preInquiry() method.
    }

    /**
     * @param PremiumInquiryVisitor $premiumInquiryVisitor
     */
    public function validateCar(PremiumInquiryVisitor $premiumInquiryVisitor)
    {
        //Implement validateCar() method.
    }

    /**
     * 询价
     * @param PremiumInquiryVisitor $premiumInquiryVisitor
     * @return Response
     * @throws Exception
     */
    public function premiumInquiry(PremiumInquiryVisitor $premiumInquiryVisitor): Response
    {
        $basePremiumInquiry = new BasePremiumInquiry($premiumInquiryVisitor);
        $basePremiumInquiry->init();

        $basePremiumInquiry->queryInit();

        //前置规则校验接口
        $vehicleInfoCheckResponse = $this->vehicleInfoCheck($basePremiumInquiry, $premiumInquiryVisitor);

        if (!$vehicleInfoCheckResponse->isSuccess()) {
            return $vehicleInfoCheckResponse;
        }

        //方案查询
        $request = new VehicleInsureQueryRequest();
        $request->setVisitor($premiumInquiryVisitor);
        $request->basePremiumInquiry = $basePremiumInquiry;
        //一轮方案查询
        $request->callFrom = 'app_query';
        $vehicleInsureQueryResponseQuery = $this->vehicleInsureQuery($request);
        if (!$vehicleInsureQueryResponseQuery->isSuccess()) {
            return $vehicleInsureQueryResponseQuery;
        }

        //二轮方案查询
        $request->callFrom = 'app_recommend';
        $vehicleInsureQueryResponseRecommend = $this->vehicleInsureQuery($request);
        if (!$vehicleInsureQueryResponseRecommend->isSuccess()) {
            return $vehicleInsureQueryResponseRecommend;
        }

        //报价
        $vehicleQuoteMainResponse = $this->vehicleQuoteMain($basePremiumInquiry, $premiumInquiryVisitor, $vehicleInsureQueryResponseQuery, $vehicleInsureQueryResponseRecommend);
        
        return $vehicleQuoteMainResponse;

    }

    /**
     * 前置规则校验接口
     * @param BasePremiumInquiry $basePremiumInquiry
     * @param PremiumInquiryVisitor $premiumInquiryVisitor
     * @return VehicleInfoCheckResponse
     */
    private function vehicleInfoCheck(BasePremiumInquiry $basePremiumInquiry, PremiumInquiryVisitor $premiumInquiryVisitor): VehicleInfoCheckResponse
    {
        $request = new VehicleInfoCheckRequest();
        $request->basePremiumInquiry = $basePremiumInquiry;
        $request->setVisitor($premiumInquiryVisitor);

        /*** @var $response VehicleInfoCheckResponse */
        $response = $this->execute($request);

        return $response;
    }

    /**
     * 方案查询接口
     * @param VehicleInsureQueryRequest $request
     * @return VehicleInsureQueryResponse
     */
    private function vehicleInsureQuery(VehicleInsureQueryRequest $request): VehicleInsureQueryResponse
    {
        /*** @var $response VehicleInsureQueryResponse */
        $response = $this->execute($request);

        return $response;
    }

    /**
     * 报价接口
     * @param BasePremiumInquiry $basePremiumInquiry
     * @param PremiumInquiryVisitor $premiumInquiryVisitor
     * @param VehicleInsureQueryResponse $vehicleInsureQueryResponseQuery
     * @param VehicleInsureQueryResponse $vehicleInsureQueryResponseRecommend
     * @return VehicleQuoteMainResponse
     * @throws Exception
     */
    private function vehicleQuoteMain(BasePremiumInquiry $basePremiumInquiry, PremiumInquiryVisitor $premiumInquiryVisitor, VehicleInsureQueryResponse $vehicleInsureQueryResponseQuery, VehicleInsureQueryResponse $vehicleInsureQueryResponseRecommend): VehicleQuoteMainResponse
    {
        $request = new VehicleQuoteMainRequest();
        $request->basePremiumInquiry = $basePremiumInquiry;
        $request->setVisitor($premiumInquiryVisitor);
        $request->queryResponse = $vehicleInsureQueryResponseQuery;
        $request->recommendResponse = $vehicleInsureQueryResponseRecommend;

        /*** @var $vehicleQuoteMainResponse VehicleQuoteMainResponse */
        $vehicleQuoteMainResponse = $this->execute($request);

        if ($vehicleQuoteMainResponse->isSuccess()) {
            $vehicleQuoteMainResponse->bindUnderWritingData($request);
        }

        return $vehicleQuoteMainResponse;
    }


    /**
     * 文件上传
     * @param UploadFileVisitor $uploadFileVisitor
     * @return UploadFileResponse
     */
    public function uploadFile(UploadFileVisitor $uploadFileVisitor): UploadFileResponse
    {
        $request = new UploadFileRequest();
        $request->setVisitor($uploadFileVisitor);

        /*** @var $response UploadFileResponse */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 核保
     * @param PremiumInquiryVisitor $premiumInquiryVisitor
     * @return Response
     */
    public function underwriting(PremiumInquiryVisitor $premiumInquiryVisitor): Response
    {
        $request = new UnderwritingRequest();
        $request->setVisitor($premiumInquiryVisitor);

        /*** @var UnderwritingResponse $underwritingResponse */
        $underwritingResponse = $this->execute($request);

        return $underwritingResponse;
    }

    /**
     * 支付
     * @param PolicyPaymentVisitor $paymentVisitor
     * @return PolicyPaymentResponse
     */
    public function policyPayment(PolicyPaymentVisitor $paymentVisitor): PolicyPaymentResponse
    {
        $request = new PolicyPaymentRequest();
        $request->setVisitor($paymentVisitor);

        /*** @var PolicyPaymentResponse $response */
        $response = $this->execute($request);

        return $response;
    }

    /**
     * 电子保单下载
     * @param EPolicyDownloadVisitor $EPolicyVisitor
     * @return EPolicyDownloadResponse
     */
    public function EPolicyDownload(EPolicyDownloadVisitor $EPolicyVisitor): EPolicyDownloadResponse
    {
        $request = new EPolicyDownloadRequest();
        $request->setVisitor($EPolicyVisitor);

        /*** @var EPolicyDownloadResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 非车电子保单下载
     * @param EPolicyDownloadVisitor $EPolicyVisitor
     * @return ENCPolicyDownloadResponse
     */
    public function NCEPolicyDownload(EPolicyDownloadVisitor $EPolicyVisitor): ENCPolicyDownloadResponse
    {
        $request = new ENCPolicyDownloadRequest();
        $request->setVisitor($EPolicyVisitor);

        /*** @var ENCPolicyDownloadResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 非车列表
     * @param NonCarProductListVisitor $nonCarProductListVisitor
     * @return NonCarProductListResponse
     * @throws Exception
     */
    public function nonCarProductList(NonCarProductListVisitor $nonCarProductListVisitor): NonCarProductListResponse
    {
        $baseRequest = new BasePremiumInquiry($nonCarProductListVisitor);
        $baseRequest->init();

        $request = new NonCarProductListRequest();
        $request->basePremiumInquiry = $baseRequest;
        $request->setVisitor($nonCarProductListVisitor);

        /*** @var $response NonCarProductListResponse */
        $response = $this->execute($request);

        return $response;
    }

    /**
     * 验车码接口
     * @param ValidateCarVisitor $validateCarVisitor
     * @return ValidateCarResponse
     */
    public function validateCarCode(ValidateCarVisitor $validateCarVisitor): ValidateCarResponse
    {
        $response = new ValidateCarResponse(200);
        $response->printerInit();
        return $response;
    }

    //-------------------------

    /**
     * 投保单状态查询
     * @param ProposalStatusVisitor $proposalStatusVisitor
     * @return Response
     */
    public function proposalStatus(ProposalStatusVisitor $proposalStatusVisitor): Response
    {
        return new Response(200);
    }

    /**
     * 撤单接口
     * @param ProposalRecallVisitor $revokeVisitor
     * @return Response
     */
    public function proposalRecall(ProposalRecallVisitor $revokeVisitor): Response
    {
        return new Response(200);
    }

    /**
     * 核保撤回
     * @param UnderwritingRecallVisitor $recallVisitor
     * @return mixed|void
     */
    public function underwritingRecall(UnderwritingRecallVisitor $recallVisitor): Response
    {
        return new Response(200);
    }

    /**
     * 保单详情
     * @param PolicyInfoVisitor $policyInfoVisitor
     * @return Response
     */
    public function policyInfo(PolicyInfoVisitor $policyInfoVisitor): Response
    {
        return new Response(200);
    }

    public function nonCarInquiry(NonCarInquiryVisitor $nonCarInquiryVisitor): Response
    {
        return new Response(200);
    }

    public function nonCarUnderwriting(NonCarInquiryVisitor $inquiryPrinter): Response
    {
        return new Response(200);
    }

    /**
     * 新车备案
     * @return void
     */
    public function newVehicleRegistration(PremiumInquiryVisitor $premiumInquiryVisitor)
    {
    }
}
